package com.admin.controller;

import com.data.entity.User;
import com.admin.service.UserService;
import com.admin.util.CommonUtil;
import com.admin.util.LogUtil;
import com.admin.util.Result;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    private Logger logger = LogUtil.getBussinessLogger();
    @Resource
    private UserService userService;

    @GetMapping("/listPage")
    public Result list(Integer limit, Integer page) {
        Result result = new Result();
        try {
            List<User> userList = userService.listPage(page, limit);
            result.setCode(Result.Code.SUCCESS);
            result.setData(userList);
            result.setCount(userService.count());
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
            return result;
    }

    @GetMapping("/getInfo")
    public Result getInfo(Long id) {
        Result result = new Result();
        try {
            User user = userService.getInfo(id);
            result.setCode(Result.Code.SUCCESS);
            result.setData(user);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }

            return result;
    }


    /**
    * 添加或修改
    * @param user
    * @return
    */
    @PostMapping("/addAndUpdate")
    public Result addAndUpdate(User user) {
        Result result = new Result();
        try {
            if (!CommonUtil.isEmpty(user.getId())) {
            //修改信息
                User user1 = userService.getInfo(user.getId());
                user1 = user;
                user1.setUpdatetime(new Date());  //更新
                userService.edit(user1);
                result.setMessage(Result.Code.SUCCESS, "操作成功！");
            } else {
                userService.add(user);
                result.setMessage(Result.Code.SUCCESS, "添加成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
        return result;
    }


    /**
    * 删除信息
    * @param id
    * @return
    */
    @GetMapping("/delete")
    public Result delete(Long id){
        Result result=new Result();
        try {
            User user = userService.getInfo(id);
            userService.delete(id);
            result.setCode(Result.Code.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            result.setCode(Result.Code.SYSTEMERROR);
        }
         return result;
    }

}
